package cn.iocoder.yudao.module.cmt.controller.admin.content;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;
import cn.iocoder.yudao.module.cmt.controller.admin.content.vo.*;
import cn.iocoder.yudao.module.cmt.dal.dataobject.content.ContentResourceDO;
import cn.iocoder.yudao.module.cmt.service.content.ContentResourceService;

@Tag(name = "管理后台 - 资源")
@RestController
@RequestMapping("/cmt/content-resource")
@Validated
public class ContentResourceController {

    @Resource
    private ContentResourceService resourceService;

    @PostMapping("/create")
    @Operation(summary = "创建资源")
    @PreAuthorize("@ss.hasPermission('cmt:content-resource:create')")
    public CommonResult<Long> createResource(@Valid @RequestBody ContentResourceSaveReqVO createReqVO) {
        return success(resourceService.createResource(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新资源")
    @PreAuthorize("@ss.hasPermission('cmt:content-resource:update')")
    public CommonResult<Boolean> updateResource(@Valid @RequestBody ContentResourceSaveReqVO updateReqVO) {
        resourceService.updateResource(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除资源")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('cmt:content-resource:delete')")
    public CommonResult<Boolean> deleteResource(@RequestParam("id") Long id) {
        resourceService.deleteResource(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除资源")
    @PreAuthorize("@ss.hasPermission('cmt:content-resource:delete')")
    public CommonResult<Boolean> deleteResourceList(@RequestParam("ids") List<Long> ids) {
        resourceService.deleteResourceListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得资源")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('cmt:content-resource:query')")
    public CommonResult<ContentResourceRespVO> getResource(@RequestParam("id") Long id) {
        ContentResourceDO resource = resourceService.getResource(id);
        return success(BeanUtils.toBean(resource, ContentResourceRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得资源分页")
    @PreAuthorize("@ss.hasPermission('cmt:content-resource:query')")
    public CommonResult<PageResult<ContentResourceRespVO>> getResourcePage(@Valid ContentResourcePageReqVO pageReqVO) {
        PageResult<ContentResourceDO> pageResult = resourceService.getResourcePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ContentResourceRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出资源 Excel")
    @PreAuthorize("@ss.hasPermission('cmt:content-resource:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportResourceExcel(@Valid ContentResourcePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ContentResourceDO> list = resourceService.getResourcePage(pageReqVO).getList();
        ExcelUtils.write(response, "资源.xls", "数据", ContentResourceRespVO.class,
                        BeanUtils.toBean(list, ContentResourceRespVO.class));
    }

}
